<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="表id">
          <a-input placeholder="请输入表id" v-decorator="['fdTableid', validatorRules.fdTableid ]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="表名称">
          <a-input placeholder="请输入表名称" v-decorator="['fdTablename', {}]"  readonly = "readonly"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="表描述">
          <a-textarea :rows="5" placeholder="请输入表描述" v-decorator="[ 'fdTabledesc', {} ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="schema">
          <a-input placeholder="请输入schema" v-decorator="['fdSchema', {}]" readonly = "readonly"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="表创建时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'fdCreatetime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <j-dict-select-tag placeholder="请输入状态"  v-decorator="['fdStatus',{} ]"  dictCode="fieldinfo_state" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <j-dict-select-tag placeholder="请选择类型"  v-decorator="['fdType',{} ]" :triggerChange="true" dictCode="t_sys_tableinfo_type" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分区字段一">
          <a-input placeholder="请输入分区字段" v-decorator="['fdLoaddt', {}]" />
        </a-form-item>
        <a-form-item
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        label="分区字段二">
        <a-input placeholder="请输入分区字段" v-decorator="['fdPartition2', {}]" />
      </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getAction } from '@/api/manage'

  export default {
    name: "SysTableinfoModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        fdTableid:{rules: [{ required: true, message: '请输入表id!' }]},
          fdStatus:{rules: [{ required: true, message: '请输入状态!' }]},
          fdType:{rules: [{ required: true, message: '请输入类型!' }]},
        },
        url: {
          add: "/meta/sysTableinfo/add",
          edit: "/meta/sysTableinfo/edit",
          etails: "/meta/sysTableinfo/etails",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'fdTableid','fdTablename','fdTabledesc','fdType','fdSchema','fdSysid','fdStatus','fdLoaddt','fdPartition2','createUser','updateUser','isDetele'))
		  //时间格式化
          this.form.setFieldsValue({fdCreatetime:this.model.fdCreatetime?moment(this.model.fdCreatetime):null})
        });

      },
      etails(fdTableId) {
        getAction(this.url.etails, {fdTableId:fdTableId}).then((res) => {
          if (res.success) {
            this.model = Object.assign({},  res.result);
            this.visible = true;
            this.$nextTick(() => {
              this.form.setFieldsValue(pick(this.model,'fdTableid','fdTablename','fdTabledesc','fdType','fdSchema','fdSysid','fdStatus','fdLoaddt','fdPartition2','createUser','updateUser','isDetele'))
              //时间格式化
              this.form.setFieldsValue({fdCreatetime:this.model.fdCreatetime?moment(this.model.fdCreatetime):null})
            });

          } else {
            this.dataSource = null
          }
        })


      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.fdCreatetime = formData.fdCreatetime?formData.fdCreatetime.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>